//
//  MBProgressHUD+TS.swift
//  Alamofire
//
//  Created by chenyusen on 2018/3/23.
//

import MBProgressHUD

public extension MBProgressHUD {
    public static var shared = MBProgressHUD()
    
    public static func show(_ text: String? = nil, view: UIView? = nil) {
        
        let aView: UIView? = view ?? UIApplication.shared.keyWindow
        if let aView = aView {
            let hud = MBProgressHUD.showAdded(to: aView, animated: true)
            hud.mode = .text;
            hud.bezelView.color = UIColor.black.withAlphaComponent(0.8)
            hud.bezelView.style = .solidColor
            hud.label.textColor = .white
            hud.label.text = text
            hud.label.numberOfLines = 0
            hud.label.preferredMaxLayoutWidth = UIScreen.main.bounds.width - 150
            hud.offset = CGPoint(x:0, y: -100)
            hud.hide(animated: true, afterDelay: 2)
        }
    }
    
    public static func showLoading(_ text: String? = nil) {
        let hud = MBProgressHUD.shared
        if #available(iOS 9.0, *) {
            let indicatorView = UIActivityIndicatorView.appearance(whenContainedInInstancesOf: [MBProgressHUD.self])
            indicatorView.color = .white
        }
        
        hud.mode = .indeterminate
        hud.label.text = text
        hud.bezelView.color = UIColor.black.withAlphaComponent(0.8)
        hud.bezelView.style = .solidColor
        hud.label.textColor = .white
        hud.label.text = text
        hud.label.numberOfLines = 0
        hud.label.preferredMaxLayoutWidth = UIScreen.main.bounds.width - 150
        hud.offset = CGPoint(x:0, y: -100)
        let view = UIApplication.shared.topViewController()?.view
        if let view = view {
            hud.frame = view.bounds
            view.addSubview(hud)
            hud.show(animated: true)
        }
    }
    
    public static func dissmiss() {
        MBProgressHUD.shared.hide(animated: true)
    }
}

